package com.leoman.spinningmachine.main;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.leoman.helper.util.AppManager;
import com.leoman.helper.util.FragmentController;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.tab1.HomeFragment;
import com.leoman.spinningmachine.tab2.MsgFragment;
import com.leoman.spinningmachine.tab3.UserFragment;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import butterknife.BindView;

/**
 * 主页
 */

public class MainActivity extends BaseActivity {

    private String[] fragmentTags = {"tab1", "tab2", "tab3"};
    private Class[] cla = {HomeFragment.class, MsgFragment.class, UserFragment.class};
    private FragmentController mFragmentController;
    @BindView(R.id.rg)
    RadioGroup radioGroup;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        AppManager.getInstance().addActivity(this);

        boolean mResultStr = SharedPreferencesUtils.getInstance().getBoolean(Constant.ISFRISTPERMISSION);
        if (Build.VERSION.SDK_INT >= 23 && !mResultStr) {
            String[] mPermissionList = new String[]{Manifest.permission.CAMERA,Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET, Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_SETTINGS, Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS, Manifest.permission.CHANGE_NETWORK_STATE,
                    Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.RECORD_AUDIO, Manifest.permission.WAKE_LOCK,
                    Manifest.permission.VIBRATE,Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN

            };
            ActivityCompat.requestPermissions(this, mPermissionList, 100);
            SharedPreferencesUtils.getInstance().putBoolean(Constant.ISFRISTPERMISSION, true);
        }
    }

    @Override
    protected void initData() {
        mFragmentController = new FragmentController(getSupportFragmentManager(), R.id.realtabcontent, fragmentTags, this);

        setFragment(0);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_tab1:
                        setFragment(0);
                        break;
                    case R.id.rb_tab2:
                        setFragment(1);
                        break;
                    case R.id.rb_tab3:
                        setFragment(2);
                        break;
                }
            }
        });

    }


    private void setFragment(int tag) {
        mFragmentController.add(cla[tag], fragmentTags[tag], null);
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                onBackPressed();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
